/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.benedicto.servlet;

import crm.benedicto.beans.Prospecto;
import crm.benedicto.dao.ProspectoDao;
import crm.benedicto.logica.ProspectoLogica;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hcornejo
 */
public class ServletProspecto extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletProspecto</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletProspecto at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        int TipoOperacion = Integer.parseInt(request.getParameter("_Operacion"));
        ProspectoLogica objController = new ProspectoLogica();

        List lProspecto;
        int IdProspecto = 0;
                
       
        if (!String.valueOf(request.getParameter("_CodigoProspecto")).equals("")) {
            IdProspecto = Integer.parseInt(request.getParameter("_CodigoProspecto"));
        }
        
        
        
        Prospecto ProspectoBE = new Prospecto();
        ProspectoBE.setDni(request.getParameter("tbDni"));
        ProspectoBE.setNombre(request.getParameter("tbNombres"));
        ProspectoBE.setApellidopaterno(request.getParameter("tbApellidoPaterno"));
        ProspectoBE.setApellidomaterno(request.getParameter("tbApellidoMaterno"));
        ProspectoBE.setFechacontacto(request.getParameter("tbFechaContacto"));
        
               

        try {
            switch (TipoOperacion) {
                case 1:
                    lProspecto = objController.ListarProspecto(ProspectoBE);
                    request.setAttribute("listaProspectos", lProspecto);
                    request.getRequestDispatcher("/ListarProspectos.jsp").forward(request, response);
                    break;
                case 2:
                    ProspectoBE.setCodigo(IdProspecto);
                    objController.EliminarProspecto(ProspectoBE);
                    ProspectoBE.setCodigo(0);
                    lProspecto = objController.ListarProspecto(ProspectoBE);
                    request.setAttribute("listaProspectos", lProspecto);
                    request.getRequestDispatcher("/ListarProspectos.jsp").forward(request, response);
                    break;
                case 3:
                    ProspectoBE.setEmail(request.getParameter("tbEmail"));
                    ProspectoBE.setTelefono(request.getParameter("tbTelefono"));
                    ProspectoBE.setCelular(request.getParameter("tbCelular"));
                    
                    objController.InsertarProspecto(ProspectoBE);
                    request.getRequestDispatcher("/ListarProspectos.jsp").forward(request, response);
                    break;
                case 4:
                case 5:
                    ProspectoBE.setCodigo(IdProspecto);
                    if (TipoOperacion == 5) {
                        ProspectoBE.setEmail(request.getParameter("tbEmail"));
                        ProspectoBE.setTelefono(request.getParameter("tbTelefono"));
                        ProspectoBE.setCelular(request.getParameter("tbCelular"));
                        ProspectoBE.setFechacontacto(request.getParameter("tbFechaContacto"));
                        objController.ActualizarProspecto(ProspectoBE);
                    }
                    lProspecto = objController.ListarProspecto(ProspectoBE);
                    request.setAttribute("objProspecto", lProspecto.get(0));
                    request.getRequestDispatcher("/MantenimientoProspecto.jsp").forward(request, response);
                    break;
                case 6:
                    lProspecto = objController.ListarProspecto(ProspectoBE);
                    request.setAttribute("listaProspectos", lProspecto);
                    request.getRequestDispatcher("/ListarProspectos.jsp").forward(request, response);
                    break;
                    
            }
        } catch (Exception ex) {
            Logger.getLogger(ServletProspecto.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
